/**
 * 鼠标事件
 * @param {String} dom 要绑定事件的Dom对象查询字符串
 * @param {Number} range 允许触发事件的最小距离
 * @param {Number} time 允许触发事件的最短时间
 */
function MouseEvent(dom, range, time, callback){
    this.dom = document.querySelector(dom);
    this.range = range;     // 超出距离才触发
    this.time = time;
    this.onclick = false;   // 鼠标是否点击到dom对象上
    this.x = null;          // 鼠标x坐标
    this.y = null;          // 鼠标y坐标
    this.now = null;
    this.callback = callback;// 鼠标事件触发后的响应
    this.init();
}

MouseEvent.prototype.init = function(){
    var dom = this.dom;
    if(dom == null){
        throw new Error("鼠标事件绑定的对象为空!");
        return;
    }
    var _this = this;
    dom.onmousedown = function(e){
        _this.onclick = true;
        _this.x = e.clientX;
        _this.y = e.clientY;
        _this.now = Date.now();
        console.log(1)
    }
    dom.onmousemove = function(e){
        if(!_this.onclick){
            return;
        }
        console.log(2)
        var rX = e.clientX - _this.x,
            rY = e.clientY - _this.y,
            rT = Date.now() - this.now;
        if((rX > _this.range || rY > _this.range) && rT > _this.time){
            _this.x = e.clientX;
            _this.y = e.clientY;
            _this.time = Date.now();
            _this.callback(rX, rY, rT);
        }
    }
    dom.onmouseup = function(){
        console.log(3)
        _this.onclick = false;
    }
    dom.onmouseout = function(){
        _this.onclick = false;
    }
}

export default MouseEvent;